package edu.whu.demo.controller;


import edu.whu.demo.domain.Supplier;
import edu.whu.demo.service.ISupplierService;
import edu.whu.demo.service.impl.SupplierServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zxj
 * @since 2023-10-18
 */
@RestController
@RequestMapping("/supplier")
public class SupplierController {
    @Autowired
    SupplierServiceImpl supplierService;

    @PostMapping("/add")
    @PreAuthorize("hasAuthority('commodity/change')")
    public ResponseEntity<Supplier> addSupp(Supplier supplier){
         Supplier supplier1 = supplierService.addSupplier(supplier);
         return ResponseEntity.ok(supplier1);
    }

    @PostMapping("/del")
    @PreAuthorize("hasAuthority('commodity/change')")
    public ResponseEntity<Void> deleteSupp(int id){
        supplierService.delSupplierById(id);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/update")
    @PreAuthorize("hasAuthority('commodity/change')")
    public ResponseEntity<Void> updateSupp(Supplier supplier){
        supplierService.updateSupplierById(supplier);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/find")
    @PreAuthorize("hasAuthority('commodity/findAllSuppliers')")
    public ResponseEntity<Supplier> findSuppById(int id){
        Supplier supplier = supplierService.findSupplierById(id);
        return ResponseEntity.ok(supplier);
    }
}

